var qId = null;
var qStatus = null;
var lightColorFlag = "_light.";
var itemParams = "";

function showInclude() {
    $("#include").show();
    $("#qresults").html("");
    $("div.message h3").html("");
    qId = null;
}

function search() {
	var THIS = this;	
    var keywords = $("#keywords").val();    
    QuestionSetService.searchUnincludedQuestions(keywords, addRows);	
    // Reset the qId.
    qId = null;
    
    function addRows(questions) {
        var results = "";
        
        for (var i = 0; i < questions.length; i++) {
            results += buildResult(questions[i]);
        }
        
        $("#qresults").html(results);
        if(results.length > 0){        	
            $("div.message h3").html(questions.length + " rows found matching your criteria");
            THIS.checkHaveStyle();
        }else{        	
            $("div.message h3").html("No Questions matched the entered criteria");
        }    
    }
}
var checkHaveStyle = function(){
	
	var includeObjs = $("div.question").children();
	var includeObjString = ",";
	includeObjs.each(function(){
		var thisId = $(this).attr("questionid");		
		includeObjString += thisId+",";		
	});	
	var includeList=includeObjString.split(",");	
	var allObjs = $("#qresults").children();	
	allObjs.each(function(){
		try{
			var ThisID = $(this).attr("id");			
			if (includeList.indexOf(ThisID)>0){
				$(this).addClass("visited");
			}	
		}catch(err){
        	
        }	
	});
}
function buildResult(question) {
    var html = "<div id = '"+question.questionId+"' onclick='qitemClick(this, " + question.questionId + ");' onmouseover='qitemHover(this);'" +
    "                 onmouseout='qitemMouseOut(this);' qStatus='" + question.status + "'>" +
    "                <table width='100%' cellspacing='0' class='data_tbody_title'>" + 
    "                    <tbody>" +
    "                        <tr>" +
    "                            <td width='12%' class='id_number'>" + question.name + "</td>" +
    "                            <td width='10%' class='id_number'>" + question.refId + "</td>" +
    "                            <td width='5%'>" + question.version + "</td>" +
    "                            <td width='15%'>" + question.name + "</td>" +
    "                            <td width='28%'>" +
    "                            " + question.text.substring(0, 30) + (question.text.length > 30 ? "..." : "") +
    "                            </td>" +
    "                            <td width='10%'>" + question.status + "</td>" +
    "                            <td width='10%'>" + question.availability + "</td>" +
    "                        </tr>" +
    "                        <tr><td colspan='4' class='tro'></td></tr>" +
    "                    </tbody>" +
    "                </table>" +
    "            </div>";

    return html;
}

function qitemHover(item) {
    $("div.data_tbody div").removeClass("hover");
    $(item).addClass("hover");
}

function qitemClick(item, id) {
    $("div.data_tbody div").removeClass("active");
    $(item).addClass("active");
    qId = id;    
    qStatus = $(item).attr("qStatus");
}

function qitemMouseOut(item) {
    $(item).removeClass("hover");
}

function closeWindow() {
    $('#include').hide();
    window.location.reload();
}

/**
 * Request the preview question set url and show the data. Used by list and view page.
 *
 * @param questionSetId: The id of the question set.
 * @param title: The title of the question set.
 */
function preview(questionSetId, title) {
    // If provide the third param, it is the iframe jquery object.--by W.P
    if (arguments[2] != undefined && arguments[2][0]) {
        var ifmObj = arguments[2];
        ifmObj.attr("src", "preview.html?id="+questionSetId);
        $(".popup_insider").each(function(){$(this).css("padding","0px")});
        $("#preview").show();
    } else {// --by W.P
        $.get("preview.html", {
            "id" : questionSetId
        }, function(data) {
            if ($("select")[0]) $("select").hide(); // fix the dropdown can be not coverd in IE6 - W.P
            $("#preview").show();
            $("#questionSetTitle").html(title);
            $("#previewContent").html(data);
        });
    }
}

/**
 * This method is used to init the buttons with name "qbtn" on editQuestions.jsp and viewQuestions.jsp
 */
function initQbtns() {
    $("[name=qbtn]").each(function () {
        $(this).attr("disabled", true);
    });
}